package com.venue.hjt.venueclass.ui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.support.design.R.id.time
import android.util.Log
import android.view.WindowManager
import android.widget.RadioGroup
import android.widget.Toast
import com.google.gson.Gson

import com.venue.hjt.venueclass.R
import com.venue.hjt.venueclass.adapter.FVenueOrderAdapter
import com.venue.hjt.venueclass.adapter.VenueComplaintAdapter
import com.venue.hjt.venueclass.adapter.VenueComplaintContentAdapter
import com.venue.hjt.venueclass.bean.API
import com.venue.hjt.venueclass.bean.VenueBean
import com.venue.hjt.venueclass.bean.VenueBookingBean
import com.venue.hjt.venueclass.db.CardDao
import com.venue.hjt.venueclass.utlis.ClassCalendar
import com.venue.hjt.venueclass.utlis.Constant
import com.venue.hjt.venueclass.utlis.OkhttPost
import com.venue.hjt.venueclass.utlis.PrefUtild
import kotlinx.android.synthetic.main.activity_venue_complaint.*
import kotlinx.android.synthetic.main.activity_venue_order.*
import okhttp3.Call

class VenueComplaintActivity : AppCompatActivity() {
    var str=""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar!!.hide()
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
        setContentView(R.layout.activity_venue_complaint)
        rb_time05.text=ClassCalendar.getStringVenueOrderComplaint(0)
        rb_time04.text=ClassCalendar.getStringVenueOrderComplaint(1)
        rb_time03.text=ClassCalendar.getStringVenueOrderComplaint(2)
        rb_time02.text=ClassCalendar.getStringVenueOrderComplaint(3)
        rb_time01.text=ClassCalendar.getStringVenueOrderComplaint(4)
        tab_menu.check(R.id. rb_time05)
        setOk(ClassCalendar.getStringVenueOrderComplaint1(0))
        val list1= CardDao.queryComplaint("0")
        val adapterContent=VenueComplaintContentAdapter(this,list1)
//        lv_complaint.adapter=adapter
        gv_complaint_r.adapter=adapterContent
        gv_complaint_r.setOnItemClickListener { adapterView, view, i, l ->
            et_content.setText(list1[i].text)
        }
        tab_menu.setOnCheckedChangeListener({ group, checkedId ->
            when (checkedId) {
                R.id.rb_time01 -> {
                    list.clear()
                    getOk(ClassCalendar.getOldDate1(-4))
                    for (i in list.indices) {
                        Log.e("ATG",list[i].groupname+" "+list[i].date)
                    }
                }
                R.id.rb_time02 -> {
                    list.clear()
                    getOk(ClassCalendar.getOldDate1(-3))

                }
                R.id.rb_time03 -> {
                    list.clear()
                    getOk(ClassCalendar.getOldDate1(-2))

                }
                R.id.rb_time04 -> {
                    list.clear()
                    getOk(ClassCalendar.getOldDate1(-1))

                }
                R.id.rb_time05 -> {
                    list.clear()
                    getOk(ClassCalendar.getOldDate1(0))
                }
            }
        })
        Thread(Runnable {
            while (i > 0) {
                handler.sendEmptyMessage(1)
                if (i <= 0) {
                    break
                }
                try {
                    Thread.sleep(1000)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
                i--
            }
            handler.sendEmptyMessage(2)
        }).start()
        iv_complaint_close1.setOnClickListener({
            i=1
        })
        bt_complaint_close.setOnClickListener({
            i=1
        })
        bt_complaint_q.setOnClickListener({
//            http://129.1.2.243/hjtgrow/public/boardApi/venue/booking/complain?sno=98&booking_id=11&text=投诉&card_no=1068668107
            if (et_content.text.toString() == ""){
                Toast.makeText(this@VenueComplaintActivity,"投诉内容不能为空",Toast.LENGTH_LONG).show()
                return@setOnClickListener
            }
            val card_no = intent.getStringExtra("card_no")
            var id=""
            list.indices
                    .filter { list[it].is_complaint }
                    .forEach { id=list[it].id }
            Log.e("ATG",API.VENUECOMPLAIN + Constant.SNO+"&booking_id="+id+"&text="+et_content.text.toString()+"&card_no="+card_no)
            OkhttPost.Okget(API.VENUECOMPLAIN + Constant.SNO+"&booking_id="+id+"&text="+et_content.text.toString()+"&card_no="+card_no, 2, object : OkhttPost.Callback{
                override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                    Toast.makeText(this@VenueComplaintActivity,"投诉失败",Toast.LENGTH_LONG).show()
                    i=1
                }

                override fun onResponse(response: String?, id: Int) {
                    Log.e("ATG",response)
                    val gson=Gson()
                    val bean = gson.fromJson(response, VenueBean::class.java)
                    if (bean.code==1){
                        i=1
                    }
                    Toast.makeText(this@VenueComplaintActivity,bean.msg,Toast.LENGTH_LONG).show()
                }

            })
        })
        lv_complaint.setOnItemClickListener { adapterView, view, po, l ->
            for (i in list.indices){
                list[i].setIs_complaint(false)
            }
            list[po].setIs_complaint(!list[po].is_complaint)
            adapter!!.notifyDataSetChanged()
        }
    }
    internal var i = 60
    internal var handler: Handler = object : Handler() {
        override fun handleMessage(msg: android.os.Message) {
            if (msg.what == 1) {
//                tv_countdown.text = "还剩" + i + "关闭界面"
                tv_complaint_countdown1.text="还剩" + i + "关闭界面"
            } else {
                this@VenueComplaintActivity.finish()
            }
        }
    }
    val list=ArrayList<VenueBookingBean.Data>()
    var adapter:VenueComplaintAdapter?=null
    fun setOk( time:String){
        val class_no = PrefUtild.getString(applicationContext, "class_no", "")
        Log.e("ATG",API.VENUEBOOKINGLIST + Constant.SNO+"/"+time)
        OkhttPost.Okget(API.VENUEBOOKINGLIST + Constant.SNO+"/"+class_no+"/"+time, 2, object : OkhttPost.Callback{
            override fun onError(call: Call, e: Exception, id: Int) {

            }
            override fun onResponse(response: String, id: Int) {
                Log.e("ATG",response)
                val gson=Gson()
                val classHour = gson.fromJson(response, VenueBookingBean::class.java)
//                            Log.e("ATG",classHour.data.venue_info.class_hour[0].start_time)

                    if (classHour.code==1){
                        list+= classHour.data
                        list[0].setIs_complaint(true)
                        adapter=VenueComplaintAdapter(this@VenueComplaintActivity,list)
//                    adapter= FVenueOrderAdapter(this@VenueOrderActivity,list)
                        lv_complaint.adapter=adapter

                    }

            }
        })
    }
    fun getOk(time:String){
        val class_no = PrefUtild.getString(applicationContext, "class_no", "")
        Log.e("ATG",API.VENUEBOOKINGLIST + Constant.SNO+"/"+class_no+"/"+time)
        OkhttPost.Okget(API.VENUEBOOKINGLIST + Constant.SNO+"/"+class_no+"/"+time, 2, object : OkhttPost.Callback{
            override fun onError(call: Call, e: Exception, id: Int) {

            }
            override fun onResponse(response: String, id: Int) {
                Log.e("ATG",response)
                val gson=Gson()
                val classHour = gson.fromJson(response, VenueBookingBean::class.java)
//                            Log.e("ATG",classHour.data.venue_info.class_hour[0].start_time)

                if (classHour.code==1){
                    list+= classHour.data
                    list[0].setIs_complaint(true)
//                    adapter=VenueComplaintAdapter(this@VenueComplaintActivity,list)
////                    adapter= FVenueOrderAdapter(this@VenueOrderActivity,list)
//                    gv_f_venue.adapter=adapter
                }
                if (adapter==null){
                    adapter=VenueComplaintAdapter(this@VenueComplaintActivity,list)
                    lv_complaint.adapter=adapter
                }else{
                    adapter!!.notifyDataSetChanged()
                }

            }
        })
    }
}
